/**
 * 
 */
package com.hirstlogics.boxarrow.model;

import static org.junit.Assert.*;

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import org.junit.Test;

import com.hirstlogics.boxarrow.box.Box;
import com.hirstlogics.boxarrow.box.AtomicBox;
import com.hirstlogics.boxarrow.xml.IO;

/**
 * @author steve
 *
 */
public class ModelTest {

	
	private static final String TEST_PATH = "/home/steve/Projects/BoxesAndArrows/testModel.xml";
	private static final String A_MODEL_FOR_UNIT_TESTING = "A Model for Unit Testing";
	private static final String TEST_MODEL = "Test Model";

	private Model makeTestModel() {
		Model model=new Model(TEST_MODEL);
		Description desc = new Description(A_MODEL_FOR_UNIT_TESTING);
		model.setDescription(desc);
		Box first = new AtomicBox("first");
		model.addBox(first);
		return model;
	}
	String xmlRender(Model target) {
		StringWriter result = new StringWriter();
		XMLOutputFactory xmlof = XMLOutputFactory.newInstance();
		try {
			XMLStreamWriter writer = xmlof.createXMLStreamWriter(result);
			target.render(writer);
		} catch (XMLStreamException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result.toString();
	}
	

	/**
	 * Test method for {@link com.hirstlogics.boxarrow.model.Model#getDescription()}.
	 */
	@Test
	public void testGetDescription() {
		Description desc = makeTestModel().getDescription();
		assertEquals(desc,new Description(A_MODEL_FOR_UNIT_TESTING));
	}

	/**
	 * Test method for {@link com.hirstlogics.boxarrow.model.Model#setDescription(com.hirstlogics.boxarrow.model.Description)}.
	 */
	@Test
	public void testSetDescription() {
		fail("Not yet implemented");
	}

	/**
	 * Test method for {@link com.hirstlogics.boxarrow.model.Model#getName()}.
	 */
	@Test
	public void testGetName() {
		assertEquals(makeTestModel().getName(),TEST_MODEL);
	}

	/**
	 * Test method for {@link com.hirstlogics.boxarrow.model.Model#setName(java.lang.String)}.
	 */
	@Test
	public void testSetName() {
		fail("Not yet implemented");
	}

	/**
	 * Test method for {@link com.hirstlogics.boxarrow.model.Model#isDirty()}.
	 */
	@Test
	public void testIsDirty() {
		fail("Not yet implemented");
	}

	/**
	 * Test method for {@link com.hirstlogics.boxarrow.model.Model#getFileName()}.
	 */
	@Test
	public void testGetFileName() {
		fail("Not yet implemented");
	}

	/**
	 * Test method for {@link com.hirstlogics.boxarrow.model.Model#isPersisted()}.
	 */
	@Test
	public void testIsPersisted() {
		fail("Not yet implemented");
	}

	/**
	 * Test method for {@link com.hirstlogics.boxarrow.model.Model#Model(java.lang.String)}.
	 */
	@Test
	public void testModel() {
		fail("Not yet implemented");
	}

	/**
	 * Test method {@link com.com.hirstlogics.boxarrow.model.Model#addBox }
	 */
	@Test
	public void testAddBox() {
		Box harold = new AtomicBox("Harold");
		Model test = makeTestModel();
		test.addBox(harold);
		String rendering = xmlRender(test);
		System.out.println(rendering);
	}
	
	/**
	 * Test method for {@link com.hirstlogics.boxarrow.model.Model#saveAs(java.lang.String)}.
	 */
	@Test
	public void testSaveAs() {
		Model testModel = makeTestModel();
		assertTrue(testModel.saveAs(TEST_PATH));
		
	}

	/**
	 * Test method for {@link com.hirstlogics.boxarrow.model.Model#save()}.
	 */
	@Test
	public void testSave() {
		Model testModel = makeTestModel();
		testModel.saveAs(TEST_PATH);
		testModel.addBox(new AtomicBox("extra"));
		assertTrue(testModel.save());
	}

	/**
	 * Test method for {@link com.hirstlogics.boxarrow.model.Model#open(java.lang.String)}.
	 */
	@Test
	public void testOpen() {
		Model testModel = Model.open(TEST_PATH);
		assertTrue(testModel!=null);
		System.out.println("=====");
		System.out.println(testModel.name);
		System.out.println(xmlRender(testModel));
		System.out.println("=====");

	}

	/**
	 * Test method for {@link com.hirstlogics.boxarrow.model.Model#info()}.
	 */
	@Test
	public void testInfo() {
		fail("Not yet implemented");
	}
	/**
	 * Test method for {@link com.hirstlogics.boxarrow.model.Description#toXML()}.
	 */
	@Test
	public void testReadXML() {
		String xmlText = "<ba:Model name='Read Model' xmlns:ba=\'http://www.hirstlogics.com/boxarrow\'><ba:Description><div>A Model created from string</div></ba:Description></ba:Model>";
		System.out.println("-----------");
		XMLInputFactory xif = XMLInputFactory.newInstance();
		StringReader input = new StringReader(xmlText);

		try {
			XMLStreamReader reader = xif.createXMLStreamReader(input);
			reader.next();
			Model d = IO.readModel(reader);
			System.out.println(d);
			System.out.println(xmlRender(d));
		} catch (XMLStreamException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("-----------");
		

	}

}
